<?php

namespace fastflow\driver\think;

use think\Config;
use think\Db;
use fastflow\driver\interfaces\toolTemplate;

class tool implements toolTemplate
{

    public static function getFieldsWithComment($param)
    {
        $config = Config::get('database');
        $schema = $config['database'];

        $table = $param['table'];
        $db_param = [$table, $schema];
        $column = '';
        if (isset($param['field']) && $param['field'] !== '') {
            $column = "AND COLUMN_NAME = '" . $param['field'] . "'";
        }
        $columns = Db::query("SELECT COLUMN_NAME as field,column_comment as comment FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = ? AND table_schema = ? " . $column, $db_param);
        $result = [];
        foreach ($columns as $item) {
            $result[] = ['field' => $item['field'], 'comment' => $item['comment']];
        }
        return $result;
    }


    public static function getBillByCondition($param)
    {
        if (isset($param)) {
			$fields = Db::getFields($param['bill']);
			$isContain = false;
			foreach($fields as $key =>$r){
                if($key == $param['column']) {
                    $isContain = true;
                };
			}
			if(!$isContain){
				return false;
			}

            $row = Db::table($param['bill'])
                ->where('id', $param['bill_id'])
                ->where($param['column'], $param['operator'], $param['value'])
                ->find();
            return $row;
        }
        return false;
    }


    public static function getBillRows($param)
    {
        if (isset($param)) {
			$isContain = false;
			$tables = Db::query("select TABLE_NAME as name,TABLE_COMMENT as comment from information_schema.tables where table_schema='" . Config::get('database')['database'] . "'");
			for ($i = 0; $i < count($tables); $i++) {
                if ($tables[$i]['name'] == $param['bill']) {
                    $isContain = true;
                    break;
                }
            }
			if(!$isContain){
				return false;
			}
			$rows = Db::table($param['bill'])->where($param['where'])->select();
            if (count($rows) == 0) {
                return false;
            }
            return $rows;
        }
        return false;
    }

    public static function getBillRowsCount($param)
    {
        if (isset($param)) {
            $isContain = false;
            $tables = Db::query("select TABLE_NAME as name,TABLE_COMMENT as comment from information_schema.tables where table_schema='" . Config::get('database')['database'] . "'");
            for ($i = 0; $i < count($tables); $i++) {
                if ($tables[$i]['name'] == $param['bill']) {
                    $isContain = true;
                    break;
                }
            }
            if(!$isContain){
                return false;
            }
            return Db::table($param['bill'])->where($param['where'])->count();
        }
        return false;
    }


    public static function setBillStatus($param)
    {
        if (isset($param)) {
            $count = Db::table($param['bill'])->update(['id' => $param['bill_id'], 'status' => $param['status']]);
            return $count;
        }
        return false;
    }


    public static function getThreadLog($param)
    {
        if (isset($param)) {
            $rows = Db::table('__FASTFLOW_THREAD_LOG__')->where($param)->order('createtime desc')->select();
            if (count($rows) == 0) {
                return false;
            }
            return $rows;
        }
        return false;
    }

    public static function getThreadLogByIds($param)
    {
        if (isset($param)) {
            $rows = Db::table('__FASTFLOW_THREAD_LOG__')->where($param['condition'])->where('id', 'in', $param['ids'])->order('createtime desc')->select();
            if (count($rows) == 0) {
                return false;
            }
            return $rows;
        }
        return false;
    }

    public static function getTableComment($param)
    {
        if (isset($param)) {
            $tables = Db::query("select TABLE_NAME as name,TABLE_COMMENT as comment from information_schema.tables where table_schema='" . Config::get('database')['database'] . "'");
            $table_comment = '';
            for ($i = 0; $i < count($tables); $i++) {
                if ($tables[$i]['name'] == $param['table']) {
                    $table_comment = $tables[$i]['comment'];
                    break;
                }
            }
            return $table_comment;
        }
        return false;
    }
}